<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="com.sinodata.bsm.common.vo.CollectTask"%>
<%@page import="java.util.List"%>
<%@page import="com.sinodata.bsm.common.vo.CollectSchedule"%>
<%@page import="com.sinodata.bsm.common.vo.CollectParam"%>
<%@page import="com.sinodata.bsm.common.vo.CollectField"%>
<%@page import="com.sinodata.bsm.common.vo.Property"%>
<%@page import="com.sinodata.bsm.common.constants.PropertyConstants"%>
<%
	CollectTask task =(CollectTask) request.getAttribute("collecttask");
	List<CollectSchedule> schedules =(List<CollectSchedule>)request.getAttribute("schedules");
	List<CollectParam> params =	(List<CollectParam>)request.getAttribute("params");
	List<CollectField> fields =(List<CollectField>)request.getAttribute("fields");
	List<Property> propertys = (List<Property>)request.getAttribute("propertys");
	String[] props =(String[]) request.getAttribute("props");
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>采集任务修改</title>
 	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>  
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/form.jsp"%>
	<%@ include file="../include/crontab.jsp"%>
	<%@ include file="../include/timeUnit.jsp"%>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/default/bsm/sub_menu.css" />
	<%@ include file="../include/table.jsp"%>
	<style type="text/css">
		.tdTitle{
				text-align:center;
				border: 1px solid #D6D3D6;
				line-height: 26px;
				height: 32px;
				color: black;
				background-color: #F6F6F6;
				color: black;
				font-weight: normal;
				overflow: hidden;
		}
		
		.tdValue{
			border: 1px solid #D6D3D6;
			
			line-height: 26px;
			height: 32px;
			padding-left: 5px;
			overflow: hidden;
		}
		
		.input_text{
			border:1px solid #D6D3D6;
			height: 20px;
			line-height:20px;
		}
		
		#task tbody td{
			white-space:normal;
		}
	</style>
	<script type="text/javascript">
		//用来保存的周期采集
		var weeks = new Array();
		function	changeScheduleType(id,i){
			var typeId = $("#"+id+"").val();
			if(typeId==1){
				$('#week'+i+'').css('display','block');  //周期
				$('#timing'+i+'').css('display','none');
			}else if(typeId==2){
				$('#week'+i+'').css('display','none');
				$('#timing'+i+'').css('display','block');
			}
		}
		
		function addRow(){
			var indexValue =weeks.length;
			var tr = $('#copyTask').html().replace('del(i)','del('+indexValue+')')
										  .replace('scheduleTypei','scheduleType'+(indexValue)+'')
								          .replace('changeScheduleType(this.id,i)','changeScheduleType(this.id,'+(indexValue)+')')
								          .replace('weeki','week'+(indexValue)+'')
								          .replace('timingi','timing'+(indexValue)+'');
			var task=$('#task tbody tr:last-child');
			task.before('<tr id="scheduleRow" name="scheduleRow" class="odd" delIndex="'+indexValue+'" >'+tr+'</tr>');
			var week = new SimpleTimeUnit("week"+(indexValue)+"");
				 	week.show();          
					week.reversal('60');
					weeks.push(week);	
			 Crontab.Method.initAddCrontab("timing"+indexValue+"","0 0 0 * * ?"); 
		}
		
		function sub(){
			if(isDel('${collecttask.id}')==true){
				getTopWin(window).getMessageBox().tip('采集任务已被删除',"");
				location.assign('collect-task!common.action?id=${resId}');
				return false;
			}
				var returnFlag = true;
				var schedules = $("#task tbody tr[name='scheduleRow']"); 
				if(schedules.length<=0){
					getTopWin(window).getMessageBox().alert("调度方式必须包含一项","提示");
					return false;
				}else{
					var weekCount = 0; //周期设置只能一个
					$.each(schedules,function(index,ob){
						var type = $($(ob).children('td')[1]).children('select').val(); //采集类型
						if(type==1){
							var week = weeks[index];
							if(week.results()==0){
								getTopWin(window).getMessageBox().alert("第"+(index+1)+"个调度时间不能为0","提示");
								returnFlag = false;
								return false;
							}
							weekCount++;
							if(weekCount>1){
								getTopWin(window).getMessageBox().alert("周期采集只能有一个","提示");
								returnFlag = false;
								return false;
							}
						}
					});
				}
				
				var collectParam = $("input[name='paramValue']");
				$.each(collectParam,function(index,ob){
					if($(ob).val()==''){
						getTopWin(window).getMessageBox().alert("采集参数不能为空","提示");
						$(this).focus();
						returnFlag = false;
						return false;
					}
				});
				
				var collectField = $("input[name='fieldName']");
				var empty = 0;
				$.each(collectField,function(index,ob){
					var val = $(ob).val();
					if(val==''){
						empty++;
					}
					
					if(empty==collectField.length){
						getTopWin(window).getMessageBox().alert("采集字段必须有一项不能为空","提示");
						returnFlag = false;
						return false;
					}
				});
				
				if(returnFlag == false){
					return false;
				}
				
				var scheduleArray ="["; //采集调度
				$.each(schedules,function(index,ob){
					var type = $($(ob).children('td')[1]).children('select').val(); //采集类型
					var time = $($(ob).children('td')[2]).children('div').children('div')[1]; //定时采集
					var schedule = "{'scheduleType':"+type+",";
					if(type==1){
						schedule+="'scheduleParam':"+weeks[index].results();
					}else if(type==2){
						var id =$(time).attr('id');
						var value = Crontab.Data.get.getDataByKey(""+id+"");
						schedule+="'scheduleParam':"+value;
					}	
						schedule+="}";
						if(index!=schedules.length-1){
							schedule+=",";
						}
						scheduleArray+=schedule;
				});
				scheduleArray+="]";
				
				var collectParamName = $("span[name='paramName']"); //采集参数
				var collectParamArry = "[";
				$.each(collectParamName,function(index,ob){
					var param = "{'paramName':'"+$(ob).text()+"'}";
					if(index!=collectParamName.length-1){
						param+=',';
					}
					collectParamArry+=param;
				});
				collectParamArry+="]";
				
				var props = $("span[name='prop']");
				var collectFieldArray  = "[";
				$.each(props,function(index,ob){
				    var propValue = $(collectField[index]).val().trim();
				    if(propValue!=""&&propValue.length>0){
				    	var field = "{'prop':"+$(ob).attr("idValue")+",'fieldName':'"+propValue+"'}";
						if(index!=props.length-1){
							field+=",";
						}
						collectFieldArray+=field;
				    }
				});
				collectFieldArray+="]";
				var taskForm = $('#taskForm');
				
				taskForm.attr('action','collect-task!update.action?id=${collecttask.id}&collectSchedules='+scheduleArray+'&paramNames='+collectParamArry+'&collectFields='+collectFieldArray);
				return true;
		}
		
			
		function del(iRow){
			var scheduleRow = $('#task #scheduleRow');
			if(scheduleRow.length>1){
				var n = 0;
				$.each(scheduleRow,function(index,ob){
					//删除保存在全局变量weeks里面的调度方式
					if($(ob).attr('delIndex')==iRow){
						$(ob).remove();
					}
					
					if(weeks[index]!=weeks[iRow]){
						weeks[n++] = weeks[index];
					}
				});
				weeks.length-=1
				
			}else{
				getTopWin(window).getMessageBox().alert("必须有一个调度方式","提示");
			}
		}
		
		//验证是否删除
		function isDel(id){
			var isFlag = false;
			$.ajax({
					type:'post',
					url:'collect-task!isDelete.action',
					data:{id:id},
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						isFlag = data;
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('检验采集任务是否删除出错!','错误');
					}
			});
			return isFlag;
		}
			
	</script>
  </head>
  
  <body>
  <form action="" id="taskForm" onsubmit="return sub();" method="post" style="width:98%;">
        修改采集配置<hr/>
	<div id="collectorInfo">
		<table class="table_eidt" >
			<tbody>
				<tr>
					<td class="td_left"  style="width:150px">资源类别采集器</td>
					<td class="td_right" style="width:350px;">
						${collector.name}
						<input name="resId" type="hidden" value="${resId}" />
					</td>
					
					<td class="td_left"  style="width:150px">是否保存</td>
					<td class="td_right">
						<input type="radio" value="0" <% if(task.getIsSave()!=1){out.println("checked='checked'");} %> name="isSave" id="no" />
						<label for="no" tabindex="1">否</label>
						&nbsp;&nbsp;
						<input type="radio" value="1" <% if(task.getIsSave()==1){out.println("checked='checked'");} %> name="isSave" id="yes" />
						<label for="yes" tabindex="1">是</label>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
	<br />
	<hr/>
	<div id="scheduleInfo">
		<div class ="toolbar" id="toolbar">
			<a href="javascript:;" class="page_add" onclick="addRow()" >新增一行</a>
		</div>
		<table class="display dataTable" id="task" cellpadding="0" cellspacing="0" > 
			<thead>
				<tr role="row">
					<th style="width:150px;">操作</th>
					<th style="width:350px;">调度方式</th>
					<th >调度时间</th>
				</tr>
			</thead>
			<tbody>
				<script >
					//设置定时采集为准许在页面显示多个
					Crontab.isClearDiv = false;
					Crontab.isSingleton = false;
				</script>
				<%
					for(int i = 0; schedules!=null&&i<schedules.size();i++){
					CollectSchedule schedule = schedules.get(i);
					Long scheduleType = schedule.getScheduleType();
				%>  
				<tr id="scheduleRow" name="scheduleRow"  class="<% if((i+1)%2==0){out.println("even");}else{out.println("odd");} %>" delIndex="<%=i %>">
					<td style="text-align:center;"><a href="#" onclick="del(<%=i %>)"><img src='../style/default/bsm/images/delete.gif' title='删除' /></a></td> 
					<td style="padding-left:4px;">
						<select id="scheduleType<%=i %>"  name="scheduleType" style="width:220px;" onchange="changeScheduleType(this.id,<%=i %>)" class="input_text" >
							<option value="<%=PropertyConstants.PROPERTY_REFRESH_PERIOD %>" <%=scheduleType.intValue()==PropertyConstants.PROPERTY_REFRESH_PERIOD?"selected":"" %>>周期采集</option>
							<option value="<%=PropertyConstants.PROPERTY_REFRESH_TIMER %>" <%=scheduleType.intValue()==PropertyConstants.PROPERTY_REFRESH_TIMER?"selected":"" %>>定时采集</option>
						</select>
					</td>
					<td >
					  <div name="scheduleTime" class="week-div">
						<div id='week<%=i %>' name="week" <%if(scheduleType.intValue()!=PropertyConstants.PROPERTY_REFRESH_PERIOD){out.println("style='display:none;'"); } %> ></div>
						<script>
				        	var week = new SimpleTimeUnit("week<%=i %>");
				        	week.show();
				        	week.reversal('<%=scheduleType.intValue()==PropertyConstants.PROPERTY_REFRESH_PERIOD?schedule.getScheduleParam():"60" %>');
				        	weeks.push(week);
			        	</script>		
						 <div id="timing<%=i %>" name="timing" <%if(scheduleType.intValue()!=PropertyConstants.PROPERTY_REFRESH_TIMER){ out.println("style='display:none;'"); }%> ></div>
					     <script >	   
					     		Crontab.Method.initAddCrontab("timing<%=i %>","<%=scheduleType.intValue()==2?schedule.getScheduleParam():"0 0 0 * * ?" %>"); 
					     </script>
					 </div>
					</td>
				</tr>
				<%
					}
				 %>  
				 
				 <tr id="copyTask" style="display:none;" >
				 	<td style="text-align:center;" ><a href="#" onclick="del(i)"><img src='../style/default/bsm/images/delete.gif' title='删除' /></a></td>
				 	<td style="padding-left:4px;">
				 		<select id="scheduleTypei" name="scheduleType" style="width:220px;" onchange="changeScheduleType(this.id,i)" class="input_text">
							<option value="<%=PropertyConstants.PROPERTY_REFRESH_PERIOD %>" selected>周期采集</option>
							<option value="<%=PropertyConstants.PROPERTY_REFRESH_TIMER %>" >定时采集</option>
						</select>
				 	</td>
				 	<td >
				 	  <div name="scheduleTime" class="week-div" >
				 		<div id='weeki'  name="week" ></div>
				 		
				 		<div id="timingi" name="timing"  style="display:none;"></div>
					 </div>
				 	</td>
				 </tr>
			</tbody>
		</table>
	</div>
	<h2 class="title-h2" onclick="accordionDiv(this)" ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>采集参数设置</h2>
	<div id="collectParamInfo" style="display: none;">
	 	<table class="table_eidt" >
			<tbody>
				  <%
					    StringBuffer paramName = new StringBuffer("");
					  	for(int i= 0;params!=null&& i<params.size();i++){
					  		CollectParam param = params.get(i);
					  %>
				<tr>
					<td class="td_left"  style="width:150px"  >
						<span class="span_alert">*</span><span id="paramName" name="paramName" ><%=param.getParamName() %></span>
					</td>
					<td class="td_right">
						<input type="text" name="paramValue"    value="<%=param.getParamValue()==null?"":param.getParamValue().replace("\"","&quot;") %>" class="input_text" style="width:350px;" />
					</td>
				</tr>
				<%} %>
			</tbody>
		</table>
	</div>
	<h2 class="title-h2" onclick="accordionDiv(this)"  ><span ><img  src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>采集字段映射设置&nbsp;&nbsp;&nbsp;<% if(props==null){out.println("<font color='red'>字段表达式读取出错！</font>");} %></h2>
	<div id="fieldInfo" style="display: none;" >
		<table class="display dataTable" cellpadding="0" cellspacing="0" >
			<thead>
				<tr role="row">
					<th  style="width:150px">指标</th>
					<th >字段表达式</th>
				</tr>
			</thead>
			<tbody>
					<%
					 for(int i = 0 ;propertys!=null&& i<propertys.size();i++){
					 	Property property = propertys.get(i);
					 	CollectField field = null;
					 	for(int t=0;t<fields.size();t++){
					 	    CollectField tempField = fields.get(t);
					 		if(tempField!=null&&property.getId().longValue()==tempField.getProp().longValue()){
					 			field = tempField;
					 			break;
					 		}
					 	}
					%>
				<tr class="<%=i%2==0?"odd":"even" %>">
					<td  style="width:150px;">
						 <span class="span_alert">*</span><span  name="prop" idValue="<%=property.getId() %>" ><%=property.getName()==null?"":property.getName() %></span>
					</td>
					<td >
						<input type="text"  value="<%=(field!=null&&field.getFieldName()!=null)?field.getFieldName().trim():""%>" id="fieldName<%=i %>"  class="input_text"  name="fieldName" maxlength="20" style="width:350px;" />
					</td>
				</tr>
					<%
					 }
					 %>
				<tr class="odd">
					<td   colspan="2" style="text-align: left;" >
						<span style="color:red;">可参照字段：</span>
						<%
						for(int r = 0 ; props!=null&&r<props.length;r++){
							String str = props[r];
							
							out.println(str);
						}
						%>
					</td>
				</tr>	
			</tbody>
		</table>
	</div>
	<div class="btn-div" >
		<input type="submit" value="保存" class="btn-4" />
		<input type="button" value="返回" onclick="history.back()" class="btn-4" />
	</div>
   </form>
  </body>
</html>
